import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '登录页',
      component: () => import('@/views/login/Login.vue'),
      meta: {
        title: '登录页'
      }
    },
    {
      path: '/home',
      name: '首页',
      component: () => import('@/views/layout/layout.vue'),
      meta: {
        title: '首页'
      },
      redirect: '/community',
      children: [
        {
          path: '/community',
          name: '社区',
          component: () => import('@/views/page/Community.vue'),
          meta: {
            title: '社区'
          }
        },
        {
          path: '/user/ceneter',
          name: '个人中心',
          component: () => import("@/views/page/UserCenter.vue"),
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/add/plan',
      name: '新增计划',
      component: () => import('@/views/add/AddPlan.vue'),
      meta: {
        title: '规划每日计划'
      }
    }
  ],
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  const token = localStorage.getItem('token')
  if (to.path !== '/' && !token) {
    next('/')
  }
  next()
})

export default router
